package com.mangguo.tingjian.user.controller;

import com.mangguo.tingjian.common.Result;
import com.mangguo.tingjian.user.controller.rsp.MainPageRsp;
import com.mangguo.tingjian.user.service.IDwUserActiveAppService;
import com.mangguo.tingjian.user.service.IDwUserActiveMiniService;
import com.mangguo.tingjian.user.service.IDwUserLiveAppService;
import com.mangguo.tingjian.user.service.IDwUserLiveMiniService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;

@RestController
@RequestMapping("/user/main")
@AllArgsConstructor
@Slf4j
public class MainPageController {

    private final IDwUserActiveAppService dwUserActiveAppService;

    private final IDwUserLiveAppService dwUserLiveAppService;

    private final IDwUserActiveMiniService dwUserActiveMiniService;

    private final IDwUserLiveMiniService dwUserLiveMiniService;

    private final ExecutorService executorService = new ThreadPoolExecutor(4, 4,

            30L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());

    @GetMapping("/data")
    public Result<MainPageRsp> getData(){
        MainPageRsp mainPageRsp = new MainPageRsp();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
        int start = Integer.valueOf(simpleDateFormat.format(new Date())) * 100;

        CountDownLatch countDownLatch = new CountDownLatch(6);
        AtomicReference<Long> appActive = new AtomicReference<>(0L);
        AtomicReference<Long> carActive = new AtomicReference<>(0L);
        AtomicReference<Long> miniActive = new AtomicReference<>(0L);
        AtomicReference<Long> appMau = new AtomicReference<>(0L);
        AtomicReference<Long> carMau = new AtomicReference<>(0L);
        AtomicReference<Long> miniMau = new AtomicReference<>(0L);
        executorService.execute(() ->{
            appActive.set(dwUserActiveAppService.countAllUniqueImei());
            countDownLatch.countDown();
        });
        executorService.execute(() ->{
            carActive.set(0L);
            countDownLatch.countDown();
        });
        executorService.execute(() ->{
            miniActive.set(dwUserActiveMiniService.countAllUniqueImei());
            countDownLatch.countDown();
        });

        executorService.execute(() ->{
            appMau.set(dwUserLiveAppService.countUniqueImeiBetweenDate(start + 1, start + 31));
            countDownLatch.countDown();
        });
        executorService.execute(() ->{
            miniMau.set(dwUserLiveMiniService.countUniqueImeiBetweenDate(start + 1, start + 31));
            countDownLatch.countDown();
        });
        executorService.execute(() ->{
            carMau.set(0L);
            countDownLatch.countDown();
        });

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            log.error("多线程获取数据异常", e);
        }


        mainPageRsp.setAppActive(appActive.get());
        mainPageRsp.setAppMau(appMau.get());
        mainPageRsp.setCarActive(carActive.get());
        mainPageRsp.setCarMau(carMau.get());
        mainPageRsp.setMiniActive(miniActive.get());
        mainPageRsp.setMiniMau(miniMau.get());
        mainPageRsp.setAllSum(appActive.get() + carActive.get() + miniActive.get());

        return Result.success(mainPageRsp);
    }
}
